/**
 * Created by aliaksei on 10/21/14.
 */

function loadCategories(categoryDivId, rootElemDivId, subElemDivId) {
  $.ajax(
      {
        url: '?listGen&act=ctg',
        type: 'post'
      }
  ).done(function (response) {
        $("#" + categoryDivId).html(response);
        listenSectionChange(rootElemDivId, subElemDivId);
      })
}

function listenSectionChange(rootElemDivId, subElemDivId) {
  $('#sectionId').change(function () {
    $.ajax(
        {
          url: '?listGen&act=lsr',
          type: 'post',
          data: {sectionId: $('#sectionId').val()}
        }
    ).done(function (response) {
          $("#" + rootElemDivId).html(response);
          listenRootChange(subElemDivId);
        })
  });
}

function listenRootChange(subElemDivId){
  $('#listRoot').change(function () {
    $.ajax(
        {
          url: '?listGen&act=lss',
          type: 'post',
          data: {listRoot: $('#listRoot').val()}
        }
    ).done(function (response) {
          $("#" + subElemDivId).html(response);
        })
  });
}
